package bootstrap.liftweb

import app.Control._
import app.server.services.ServiceFactory
import app.ui.AppView
import net.liftweb.common._
import net.liftweb.http._
import net.liftweb.sitemap._
import net.liftweb.util._
import restlike.demo.shoreditchairways.{Finance, Reservations, Seating, Ticketing}
import restlike.shoreditch.Shoreditch

class Boot extends Loggable {
  def boot() {
    logger.info("Lift is booting ...")
    fatalOnFailure(unsafeBoot())(logger)
  }

  private def unsafeBoot() {
    LiftRules.addToPackages("app")

    val entries = List(
      Menu(S ? "Shoreditch") / "index"
    )

    LiftRules.setSiteMap(SiteMap(entries: _*))

    LiftRules.viewDispatch.append {
      case List("index") ⇒ Left(() ⇒ Full(AppView()))
    }
    
    LiftRules.uriNotFound.prepend(NamedPF("404handler") {
      case (req, failure) ⇒ NotFoundAsTemplate(ParsePath(List("404"), "html", false, false))
    })

    LiftRules.early.append(_.setCharacterEncoding("UTF-8"))
    LiftRules.useXhtmlMimeType = false
    LiftRules.stripComments.default.set(() ⇒ false)
    LiftRules.explicitlyParsedSuffixes += "csv"

    LiftRules.statelessDispatch.append(Shoreditch)

    //Shoreditch Airways Demo .. these should maybe in a different app ...
    LiftRules.statelessDispatch.append(restlike.demo.shoreditchairways.Booking)
    LiftRules.statelessDispatch.append(Reservations)
    LiftRules.statelessDispatch.append(Finance)
    LiftRules.statelessDispatch.append(Ticketing)
    LiftRules.statelessDispatch.append(Seating)

    //Royal Bank of Shoredithc Demo .. these should maybe in a different app ...
//    LiftRules.statelessDispatch.append(Booking)
//    LiftRules.statelessDispatch.append(Pricing)
//    LiftRules.statelessDispatch.append(Trading)
//    LiftRules.statelessDispatch.append(Cleared)
//    LiftRules.statelessDispatch.append(Seating)

    ServiceFactory.startup()

    logger.info("Lift has Booted.")
  }
}